package com.xyy.saas.payment.channel.login.core.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xyy.saas.payment.adpater.RouteManager;
import com.xyy.saas.payment.cashier.core.service.CashierService;
import com.xyy.saas.payment.cashier.core.vo.ChannelLoginVO;
import com.xyy.saas.payment.channel.login.core.bo.ChannelLoginResultBO;
import com.xyy.saas.payment.channel.login.core.service.ChannelLoginService;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.ChannelEnum;
import com.xyy.saas.payment.dao.model.PrePayOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wangtianqi
 * @date 2020/7/6 17:34
 */
@Slf4j
@Service
public class ChannelLoginServiceImpl implements ChannelLoginService {

    @Autowired
    private RouteManager routeManager;
    @Autowired
    private CashierService cashierService;

    @Override
    public ChannelLoginResultBO login(ChannelLoginVO channelLoginVO) throws PaymentException {
        PrePayOrder prePayOrder = cashierService.getPrePayOrderByPayNoToken(channelLoginVO.getPayNo());
        channelLoginVO.setBusinessOrderType(prePayOrder.getBusinessOrderType());
        channelLoginVO.setAccountId(prePayOrder.getReceiverId());
        channelLoginVO.setBusinessType(prePayOrder.getBusinessType());
        channelLoginVO.setPNo(prePayOrder.getPayNo());
        log.info("mini login:{},channelLoginVO:{}",prePayOrder.getPayNo(),JSONObject.toJSONString(channelLoginVO));
        if (!ChannelEnum.JDAGGREGATE_WX.getCode().equals(channelLoginVO.getChannel())) {
            if (channelLoginVO.getChannel().contains(ChannelEnum.WX.getCode())) {
                log.info("不为京东聚合支付且包含微信，转渠道为微信渠道 {}", JSONObject.toJSONString(channelLoginVO));
                channelLoginVO.setChannel(ChannelEnum.WX.getCode());
            }
        }
        return routeManager.login(channelLoginVO, channelLoginVO.getChannel());
    }

}
